package com.xmut.mall.order.controller;

import java.util.Arrays;
import java.util.Map;

import cn.hutool.core.map.MapUtil;
import com.xmut.mall.order.annotation.CheckJwt;
import com.xmut.mall.order.dto.OrderComplaintDTO;
import com.xmut.mall.order.dto.OrderComplaintOperationParams;
import com.xmut.mall.order.dto.OrderComplaintSearchParams;
import com.xmut.mall.order.myenum.OrderComplaintStatusEnum;
import com.xmut.mall.order.utils.RespBean;
import com.xmut.mall.order.vo.PageVO;
import com.xmut.mall.order.vo.StoreAppealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xmut.mall.order.entity.OrderComplaintEntity;
import com.xmut.mall.order.service.OrderComplaintService;
import com.xmut.mall.order.utils.PageUtils;
import com.xmut.mall.order.utils.R;

@RestController
@RequestMapping("order/ordercomplaint")
public class OrderComplaintController {
    @Autowired
    private OrderComplaintService orderComplaintService;

    /**
     * 商家申诉
     * @return
     */
    @CheckJwt
    @PutMapping("/appeal")
    public RespBean appeal(@RequestBody StoreAppealVO vo){
        return orderComplaintService.appeal(vo);
    }

    /**
     * 修改状态
     * @param params
     * @return
     */
    @CheckJwt
    @PutMapping("/status")
    public RespBean updateStatus(@RequestBody OrderComplaintOperationParams params){
        orderComplaintService.updateOrderComplainByStatus(params);

        return RespBean.success("成功");
    }

    /**
     * 投诉仲裁
     * @return
     */
    @CheckJwt
    @PutMapping("/complete")
    public RespBean complete(@RequestBody Map<String,Object> params){
        Long id = MapUtil.getLong(params, "id");
        String arbitrationResult = MapUtil.getStr(params, "arbitrationParams");
        //新建对象
        OrderComplaintOperationParams orderComplaintOperationParams = new OrderComplaintOperationParams();
        orderComplaintOperationParams.setComplainId(id);
        orderComplaintOperationParams.setArbitrationResult(arbitrationResult);
        orderComplaintOperationParams.setComplainStatus(OrderComplaintStatusEnum.COMPLETE.name());
        //修改状态
        orderComplaintService.updateOrderComplainByStatus(orderComplaintOperationParams);

        return RespBean.success("成功");
    }

    /**
     * 取消订单投诉
     * @param id
     * @return
     */
    @CheckJwt
    @PutMapping("/cancelOrderComplaint/{id}")
    public RespBean cancelOrderComplaint(@PathVariable Long id){
        return orderComplaintService.cancelOrderComplaint(id);
    }

    /**
     * 获取投诉详情
     * @param id
     * @return
     */
    @CheckJwt
    @GetMapping("/getOrderComplaintDetail/{id}")
    public RespBean getOrderComplaintDetail(@PathVariable Long id){
        return orderComplaintService.getOrderComplaintDetail(id);
    }

    /**
     * 获取订单投诉的分页数据
     * @param params
     * @return
     */
    @CheckJwt
    @PostMapping("/getOrderComplaintByPage")
    public RespBean getOrderComplaintByPage(@RequestBody OrderComplaintSearchParams params){
        PageVO vo = new PageVO();
        BeanUtils.copyProperties(params,vo);
        return orderComplaintService.getOrderComplaintByPage(vo,params);
    }

    /**
     * 获取当前投诉订单项的id
     * @param itemId
     * @return
     */
    @CheckJwt
    @GetMapping("/getOrderComplaintItem/{itemId}")
    public RespBean getOrderComplaintItem(@PathVariable Long itemId){
        return orderComplaintService.getOrderComplaintItem(itemId);
    }

    /**
     * 添加订单投诉
     * @param dto
     * @return
     */
    @CheckJwt
    @PostMapping("/addOrderComplaint")
    public RespBean addOrderComplaint(@RequestBody OrderComplaintDTO dto){
        return orderComplaintService.addOrderComplaint(dto);
    }

}
